// ignore_for_file: public_member_api_docs, sort_constructors_first
import 'package:auto_route/auto_route.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class CommonStringPicker extends StatelessWidget {
  const CommonStringPicker({
    super.key,
    required this.pickItems,
    required this.onPickConfirm,
  });

  // 数据源
  final List<String> pickItems;
  //  事件
  // 选择完成回调
  final Function(String) onPickConfirm;

  @override
  Widget build(BuildContext context) {
    String selectedValue = '';
    return SizedBox(
      width: double.infinity,
      height: 340.w,
      child: Column(
        children: [
          Container(
            decoration: BoxDecoration(
              border: Border(
                bottom: BorderSide(color: const Color(0xffE6E8EC), width: 1.w),
              ),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                GestureDetector(
                  onTap: () => context.pop(),
                  child: Container(
                    padding: EdgeInsets.symmetric(horizontal: 20.w),
                    height: 50.w,
                    alignment: Alignment.center,
                    child: Text(
                      '取消',
                      style: TextStyle(
                        fontSize: 16.sp,
                        color: Colors.blueAccent,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                ),
                GestureDetector(
                  onTap: () {
                    context.pop();
                    onPickConfirm(
                      selectedValue.isEmpty ? pickItems[0] : selectedValue,
                    );
                  },
                  child: Container(
                    padding: EdgeInsets.symmetric(horizontal: 20.w),
                    height: 50.w,
                    alignment: Alignment.center,
                    child: Text(
                      '确定',
                      style: TextStyle(
                        fontSize: 16.sp,
                        color: Colors.blueAccent,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
          Expanded(
            child: CupertinoPicker(
              itemExtent: 44.w,
              onSelectedItemChanged: ((position) {
                selectedValue = pickItems[position];
              }),
              children:
                  pickItems.map((item) {
                    return Center(
                      child: Text(
                        item,
                        style: TextStyle(
                          fontSize: 16.sp,
                          color: const Color(0xff333333),
                        ),
                      ),
                    );
                  }).toList(),
            ),
          ),
        ],
      ),
    );
  }
}
